ADVERTENCIA : Este programa tiene un error de lógica. La vaca tiene hambre y quiere comerse el heno, ¡pero no pasa nada!
REGLA: Un controlador de eventos vincula un sprite a una función que se llama cada vez que se activa el evento.
Hacer clicCorre y mira lo que pasa. Mire en el código para ver dónde debería estar comiendo el heno la vaca.
Arregle el programa agregando un controlador de eventos a la función main() para que ocurra el evento de colisión.
Hacer clicEjecute para probar si arregló el programa. Cuando esté arreglado, haga clic enEnviar ysiguiente
To navigate the page using the TAB key, first press ESC to exit the code editor.
def add_sprite():
""" Add cow sprite to the stage and set its speed """
cow = codesters.Sprite("cow",-400, -150)
cow.set_x_speed(3)
return cow
def setup_stage():
""" Set up the stage with background and sprites """
stage.set_background("barn")
stage.disable_right_wall()
hay = codesters.Sprite("hay",-150,-150)
hay = codesters.Sprite("hay",0,-150)
hay = codesters.Sprite("hay",150,-150)
def collision(sprite, hit_sprite):
""" Collision event for the cow to eat hay """
if hit_sprite.get_image_name() == "hay":
sprite.say("yum!",.2)
hit_sprite.hide()
def main():
""" Sets up the program and calls other functions """
setup_stage()
sprite = add_sprite()
main()
t = codesters.Teacher()
defs = t.find_block("def")
collision_event_handlers = t.find_text("event_collision")
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
def find_new_function(ignore_list, def_list):
""" Returns tuples of (line_number, function def) for any functions that aren't specified in ignore list """
return_list = []
for d in def_list:
count = 0
for i in ignore_list:
if i in d[1]:
break
else:
count += 1
if count == len(ignore_list):
return_list.append((d[0],d[1]))
return return_list
try:
tval2 = collision_event_handlers[0][1]
tval2_line_num = collision_event_handlers[0][0]
tval2_indent = t.get_indent_at_line(tval2_line_num)
except:
tval2 = "DNE"
tval2_line_num = "DNE"
tval2_indent = "DNE"
try:
above_def, distance = get_above_def(tval2_line_num, defs)
except:
above_def = "DNE"
distance = "DNE"
t1 = TestObjective()
t1.add_success("sprite." in tval2 and "(collision)" in tval2, "Great job!")
t1.add_failure(tval2 == "DNE", "Oops, did you add a Collision Event Handler to the main() function?")
t1.add_failure("sprite." not in tval2, "Oops, make sure the event handler is connecting to sprite!")
t1.add_failure("(collision)" not in tval2, "Oops, make sure the function in the event handler is collision?")
t2 = TestObjective()
t2.add_success("main" in above_def and tval2_indent == 4, "Great job!")
t2.add_failure("main" not in above_def, "Did you place Collision Event Handler within main()?")
t2.add_failure(tval2_indent < 0 or tval2_indent > 4, "Make sure that the event handler is indented once within main()!")
tester = TestManager()
tester.add_test_list([t1, t2])
tester.run_tests()
tester.display_first_feedback()
Are you already running a Codesters project in another tab or window?
Micro:bit can only connect to one web page at a time.
Try stopping other Codesters projects or closing
other tabs or windows that may be using your Micro:bit.
If that doesn't fix the problem try disconnecting your Micro:bit,
reloading this page, and reconnecting your Micro:bit.